import java.util.ArrayList;
public class Turret extends Actor{
	private static int price = 100;
	protected int damage;
	protected int attackspeed;
	private int sellprice, upgradeprice, range, numupgrades;
	protected int cooldown;
	private Enemy target;
	private ArrayList<Location> path;
	public Turret(ArrayList<Location> pa){
		path = pa;
		target = null;
		range = 1;
		sellprice = price /2;
		upgradeprice = price/4;
		attackspeed = 0;
		damage = 1;
		numupgrades = 0;
		cooldown = attackspeed;
	}
	public boolean upgradable(){
		if(numupgrades == 4)
			return false;
		else
			return true;
	}
	public static int getPrice(){
		return price;
	}
	public void setValues(int da, int sp, int ra){
		damage = da;
		attackspeed = sp;
		range = ra;
	}
	public void setPrice(int pr){
		price = pr;
	}
	public int getSellPrice(){
		return sellprice;
	}
	public int getUpgradePrice(){
		return upgradeprice;
	}
	public void upgrade(){
		sellprice = sellprice+(upgradeprice/2);
		upgradeprice = upgradeprice + (price/4); 
	}
	public ArrayList<Enemy> getEnemies(){
		ArrayList<Enemy> enemies = new ArrayList();
		int srow, scol;
		srow = getLocation().getRow() - range;
		scol = getLocation().getCol() - range;
		for(int r =srow;r<=srow +(range*2); r++){
			for(int c = scol;c<=scol+(range*2);c++){
				Location loc = new Location(r,c);
				if(getGrid().isValid(loc)&&getGrid().get(loc) instanceof Enemy)
					enemies.add((Enemy) getGrid().get(loc));
			}
		}
		return enemies;
	}
	public void act(){
		if(cooldown == -1){
			cooldown = attackspeed;
		}
		if(cooldown == attackspeed){
			ArrayList<Enemy>enemies = new ArrayList();
			enemies = getEnemies();
			Enemy enemy = aim(enemies);
			attack(enemy);
		}
		cooldown--;
	}
	public Enemy aim(ArrayList<Enemy> enemies){
		if(target != null){
			for(int x = 0;x<enemies.size();x++){
				if(enemies.get(x).equals(target))
					return enemies.get(x);
		}	
		}
		for(int x = path.size()-1; x>=0;x--){
			for(int y = 0;y<enemies.size();y++){
				if(enemies.get(y).getLocation().equals(path.get(x))){
					target = enemies.get(y);
					return enemies.get(y);
				}
			}
		}
		return null;
	}
	public String getUpGradeInfo(){
		return "Increases the Range and Damage";
	}
	public void attack(Enemy enemy){
		if(enemy!=null){
			setDirection(getLocation().getDirectionToward(enemy.getLocation()));
			enemy.takeDamage(damage);
		}
		}
}
